package entity;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import jpa.exceptions.NonexistentEntityException;
//import controller.LogicBarang;

public class DaftarBerita {

    public DaftarBerita() {
        emf = Persistence.createEntityManagerFactory("persistence");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public Berita getBerita(Long idBerita) {
        Berita berita = null;
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM Berita AS o WHERE o.id=:ib");
            q.setParameter("ib", idBerita);
            //q.setParameter("pswd", password);
            berita = (Berita) q.getSingleResult();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return berita;
    }

    public List<Berita> getBerita() {
        List<Berita> berita = new ArrayList<Berita>();

        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("SELECT object(o) FROM Berita AS o");
            berita = q.getResultList();

        } finally {
            if (em != null) {
                em.close();
            }
        }
        return berita;
    }

    public void editBerita(Berita berita) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.merge(berita);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void addBerita(Berita berita) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(berita);
            em.getTransaction().commit();
        } catch (Exception e) {
            em.getTransaction().rollback();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void deleteBerita(Long id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Berita berita;
            try {
                berita = em.getReference(Berita.class, id);
                berita.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The berita with id " + id + " no longer exists.", enfe);
            }
            em.remove(berita);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }
}
